import axios from "axios";
import { Message } from "element-ui";

const SUCCESS_CODE = 200;

const instance = axios.create({
  baseURL: window.location.origin,
  timeout: 5 * 1000,
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

// request interceptor
instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    // do something with request error
    return Promise.reject(error);
  }
);

// response interceptor
instance.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */
  (response) => {
    const res = response.data;
    if (res.status) {
      return res.status !== SUCCESS_CODE ? Promise.reject(res) : res;
    }
    if (res.error) {
      return Promise.reject(res.error);
    }
    return res.value;
  },
  (error) => {
    Message({
      message: "请求出错了",
      type: "error",
      duration: 5 * 1000,
    });
    return Promise.reject(error);
  }
);

export default instance;
